package servlet;

import dao.AddressDao;
import dao.GoodsDao;
import dao.OrderDao;
import entity.Address;
import entity.Goods;
import entity.Order;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectOrder")
public class SelectOrderServlet extends HttpServlet {
	
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		final int SIZE = 3;
		Integer page = null;
		try {
			page = Integer.parseInt(request.getParameter("page"));
		} catch (Exception e) {
			page = 1;
		}
		int start = (page - 1)*SIZE;
		OrderDao dao = new OrderDao();
		List<Order> list = dao.queryOrderPage(start, SIZE,user.getUserId());
		int total = dao.total_order(user.getUserId());
		int pageNum = total / SIZE;
		if (total % SIZE != 0) {
			pageNum += 1;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 1; i <= pageNum; i++) {
			String str = "<a href=\"#url\">第" + i + "页</a>&nbsp;&nbsp;";
			str = str.replace("#url", request.getContextPath() + "/selectOrder?page=" + i);
			System.out.println("/selectOrder?page=" + i);
			if (page == i) {
				str = "第" + i + "页&nbsp;&nbsp;";
			}
			sb.append(str);
		}
		request.getSession().setAttribute("page", sb.toString());
		request.getSession().setAttribute("myOrder", list);
		response.sendRedirect("frontend/order.jsp");
	}


}
